# -*- coding: utf-8 -*-
"""
Created on Sat Sep  7 15:10:05 2024

@author: Hasee
"""
import random
import pygame

#屏幕大小
#SCREEN_RECT = pygame.Rect(0,0,480,700)
SCREEN_RECT = pygame.Rect(0,0,450,800)
#帧
FRAME = 60
#创建敌机的定时器常量
CREATE_ENEMY_EVENT = pygame.USEREVENT
V = 4
COUNT = 3

class GameSprite(pygame.sprite.Sprite):
    """
    飞机大战游戏精灵
    """
    def __init__(self,image_name,speed=2):
        super().__init__()
        self.image = pygame.image.load(image_name)
        self.rect= self.image.get_rect()
        self.speed = speed
        
    def update(self):
        self.rect.y += self.speed
        
class Background(GameSprite):    
        
    
    def __init__(self,flag=False):
        super().__init__("./images/background2.jpg",2)
        if flag : 
            self.rect.y = -self.rect.height
        
    
    
    def update(self):
        super().update()
        
        if self.rect.y >= SCREEN_RECT.height:
            self.rect.y = -self.rect.height
            
class Enemy(GameSprite):
    def __init__(self):
        super().__init__("./images/enemy1.png")
        self.speed = random.randint(3, 4)
        
        self.bottom = 0
        max_x = SCREEN_RECT.width-self.rect.width
        self.rect.x = random.randint(0, max_x)
        
    def update(self):
        super().update()
        if self.rect.y >= SCREEN_RECT.height:
            #print("飞出屏幕，需要从精灵组删除")
            self.kill()
            
    def __del__(self):
        #print("敌机挂了%s"%self.rect)
        pass
        
        
        
class Hero(GameSprite):
    
    def __init__(self):
        super().__init__("./images/me1.png",0)
        
        self.rect.centerx = SCREEN_RECT.centerx
        self.rect.bottom = SCREEN_RECT.bottom-120
        
        self.bullet_group = pygame.sprite.Group()
        
        
    #1是左右2是上下
    def update(self,flag):
        if flag == 1:
            self.rect.x += self.speed
        else:
            self.rect.y += self.speed
            
        if self.rect.x < 0:
            self.rect.x = 0
        if self.rect.right >SCREEN_RECT.right:
            self.rect.right = SCREEN_RECT.right
        if self.rect.y<0:
            self.rect.y = 0
        if self.rect.bottom>SCREEN_RECT.bottom:
            self.rect.bottom = SCREEN_RECT.bottom
        
            
    def fire(self):        
        for i in range(1,COUNT+1):
            bullet = Bullet()
            bullet.rect.bottom = self.rect.y-20*i
            bullet.rect.centerx = self.rect.centerx
            self.bullet_group.add(bullet)
    
            
class Bullet(GameSprite):
    
    def __init__(self):
        super().__init__("./images/bullet1.png",-3)
    def __update(self):
        super().update()
        
        if self.rect.bottom< 0 :
            self.kill()
    def __del__(self):
        print("子弹被销毁")
    
    